#include "widget_play.h"
#include "rules.h"
#include "units.h"
#include "ai.h"
#include <sstream>
#include <fstream>
#include <iostream>
#include <cmath>



map_square::map_square( QGraphicsItem* parent, QGraphicsScene* scene ): QGraphicsPixmapItem( parent, scene )
{
	this->setAcceptHoverEvents( true );
};



void map_square::mousePressEvent( QGraphicsSceneMouseEvent* event )
{
	game_gui* ggui = game_gui::get_instance();
	if ( event->button() == Qt::LeftButton )
	{
		if ( ggui->g->play_w->selected_unit != 0 )
		{
			ggui->g->play_w->selected_unit->unselect();
		}
	}
	else if ( event->button() == Qt::RightButton )
	{
		if ( ( ggui->g->play_w->selected_unit != 0 ) && ( game::get_instance()->can_play( ggui->g->play_w->selected_unit->u->get_player() ) ) )
		{	
			ggui->g->play_w->forward_command( ggui->g->play_w->selected_unit->u->get_player(), game::create_command( "MOV", ggui->g->play_w->selected_unit->u->get_id(), ( unsigned int )( this->pos().x() / parameters::get_instance()->gui_parameters[ "ICON_W" ] ), ( unsigned int )( this->pos().y() / parameters::get_instance()->gui_parameters[ "ICON_H" ] ) ) );
		}
	}
};



void map_square::hoverEnterEvent( QGraphicsSceneHoverEvent* event )
{
	game_gui* ggui = game_gui::get_instance();
	if ( ( ggui->g->play_w->selected_unit != 0 ) && ( game::get_instance()->can_play( ggui->g->play_w->selected_unit->u->get_player() ) ) )
	{
		std::queue< std::pair< unsigned int, unsigned int > >* pth = new std::queue< std::pair< unsigned int, unsigned int > >;
		int cost = test_move( pth, ggui->g->play_w->selected_unit->u, ( unsigned int )( this->pos().x() / parameters::get_instance()->gui_parameters[ "ICON_W" ] ), ( unsigned int )( this->pos().y() / parameters::get_instance()->gui_parameters[ "ICON_H" ] ) );
		if ( cost != -1 )
		{
			ggui->g->play_w->path = new p( pth, cost );
		}
	}
};




void map_square::hoverLeaveEvent( QGraphicsSceneHoverEvent* event )
{
	game_gui* ggui = game_gui::get_instance();
	if ( ggui->g->play_w->path != 0 )
	{
		delete ggui->g->play_w->path;
		ggui->g->play_w->path = 0;
	}
};



map_bonus::map_bonus( bonus* b, QGraphicsItem* parent, QGraphicsScene* scene ): QGraphicsPixmapItem( parent, scene )
{
	bon = b;
	this->setPixmap( *game_gui::get_instance()->graphics->bonus_pixmaps[ b->get_name() ] );
	this->setZValue( 7 );
	this->setShapeMode( QGraphicsPixmapItem::BoundingRectShape );
};



map_unit::map_unit( unit* un, QGraphicsItem* parent, QGraphicsScene* scene ): QGraphicsPixmapItem( parent, scene )
{
	game_gui* ggui = game_gui::get_instance();
	u = un;
	this->setPixmaps( ggui->graphics->units_pixmaps[ un->get_name() ][ ggui->graphics->get_color( un->get_player()->get_id() ) ].first, ggui->graphics->units_pixmaps[ un->get_name() ][ ggui->graphics->get_color( un->get_player()->get_id() ) ].second );
	this->setFireSound( ggui->graphics->units_sounds[ un->get_name() ][ "Fire" ] );
	this->setNormalPixmap();
	this->setShapeMode( QGraphicsPixmapItem::BoundingRectShape );
	this->setZValue( 10 );
	this->setAcceptHoverEvents( true );
};



void map_unit::select()
{
	game_gui* ggui = game_gui::get_instance();
	if ( ggui->g->play_w->selected_unit != 0 )
	{
		ggui->g->play_w->selected_unit->unselect();
	}
	ggui->g->play_w->selected_unit = this;
	ggui->g->play_w->update_selected_unit_status( this->u );
	this->setSelectedPixmap();
};



void map_unit::unselect()
{
	game_gui* ggui = game_gui::get_instance();
	this->setNormalPixmap();
	ggui->g->play_w->selected_unit = 0;
	ggui->g->play_w->update_selected_unit_status( 0 );
	if ( ggui->g->play_w->path != 0 )
	{
		delete ggui->g->play_w->path;
		ggui->g->play_w->path = 0;
	}
	if ( ggui->g->play_w->fire_line != 0 )
	{
		delete ggui->g->play_w->fire_line;
		ggui->g->play_w->fire_line = 0;
	}

};



void map_unit::setPixmaps( QPixmap* n, QPixmap* s )
{
	this->normal_pixmap = n;
	this->selected_pixmap = s;
};



void map_unit::setNormalPixmap()
{
	this->setPixmap( *normal_pixmap );
};



void map_unit::setSelectedPixmap()
{
	this->setPixmap( *selected_pixmap );
};



void map_unit::setFireSound( QSound* s )
{
	fire_sound = s;
};


void map_unit::playFireSound()
{
	fire_sound->play();
};



void map_unit::mousePressEvent( QGraphicsSceneMouseEvent* event )
{
	game_gui* ggui = game_gui::get_instance();
	if ( event->button() == Qt::LeftButton )
	{
		this->select();
	}
	else if ( event->button() == Qt::RightButton )
	{
		if ( ( ggui->g->play_w->selected_unit != 0 ) && ( game::get_instance()->can_play( ggui->g->play_w->selected_unit->u->get_player() ) ) )
		{
			ggui->g->play_w->forward_command( ggui->g->play_w->selected_unit->u->get_player(), game::create_command( "ATT", ggui->g->play_w->selected_unit->u->get_id(), ( unsigned int )( this->pos().x() / parameters::get_instance()->gui_parameters[ "ICON_W" ] ), ( unsigned int )( this->pos().y() / parameters::get_instance()->gui_parameters[ "ICON_H" ] ) ) );
		}
	}
};



void map_unit::hoverEnterEvent( QGraphicsSceneHoverEvent* event )
{
	parameters* param = parameters::get_instance();
	game_gui* ggui = game_gui::get_instance();
	if ( ( ggui->g->play_w->selected_unit != 0 ) && ( game::get_instance()->can_play( ggui->g->play_w->selected_unit->u->get_player() ) ) )
	{
		if ( test_fire( ggui->g->play_w->selected_unit->u, ( unsigned int )( this->pos().x() / param->gui_parameters[ "ICON_W" ] ), ( unsigned int )( this->pos().y() / param->gui_parameters[ "ICON_H" ] ) ) )
		{
			ggui->g->play_w->fire_line = new QGraphicsLineItem( ggui->g->play_w->selected_unit->pos().x() + param->gui_parameters[ "ICON_W" ] / 2, ggui->g->play_w->selected_unit->pos().y() + param->gui_parameters[ "ICON_H" ] / 2, this->pos().x() + param->gui_parameters[ "ICON_W" ] / 2, this->pos().y() + param->gui_parameters[ "ICON_H" ] / 2 );
			ggui->g->play_w->fire_line->setZValue( 100 );
			ggui->g->play_w->fire_line->setPen( *ggui->graphics->fire );
			ggui->g->play_w->gs->addItem( ggui->g->play_w->fire_line );
		}
	}
};



void map_unit::hoverLeaveEvent( QGraphicsSceneHoverEvent* event )
{
	game_gui* ggui = game_gui::get_instance();
	if ( ggui->g->play_w->fire_line != 0 )
	{
		delete ggui->g->play_w->fire_line;
		ggui->g->play_w->fire_line = 0;
	}
};



p::p( std::queue<std::pair< unsigned int, unsigned int > >* pth, unsigned int cost = 0 )
{
	if ( cost != 0 )
	{
		cost_set = true;
	}
	else
	{
		cost_set = false;
	}
	game_gui* ggui = game_gui::get_instance();
	unsigned int x;
	unsigned int y;
	while( !pth->empty() )
	{
		x = pth->front().first;
		y = pth->front().second;
		pth->pop();
		if ( pth->size() > 0 )
		{
			QGraphicsPixmapItem* ps = new QGraphicsPixmapItem( *ggui->graphics->p );
			ps->setZValue( 100 );
			ps->setShapeMode( QGraphicsPixmapItem::BoundingRectShape );
			ggui->g->play_w->gs->addItem( ps );
			ps->setPos( x * parameters::get_instance()->gui_parameters[ "ICON_W" ], y * parameters::get_instance()->gui_parameters[ "ICON_H" ] );
			s.push( ps );
		}
	}
	if ( cost_set )
	{
		cost_l = new QGraphicsTextItem();
		std::ostringstream sout;
		sout << cost;
		cost_l->setFont( QFont( "Arial", 7, 60 ) );
		cost_l->setDefaultTextColor( QColor( 0, 0, 255 ) );
		cost_l->setPlainText( sout.str().c_str() );
		cost_l->setPos( s.front()->pos() );
		cost_l->setZValue( 150 );
		cost_l->setAcceptHoverEvents( false );
		ggui->g->play_w->gs->addItem( cost_l );
	}
};



p::~p()
{
	while( !s.empty() )
	{
		delete( s.front() );
		s.pop();
	}
	if ( cost_set )
		delete cost_l;
};



play_widget::play_widget( QWidget* parent, Qt::WindowFlags flags ): QWidget( parent, flags )
{
	connect( this, SIGNAL( send_command( player*, std::string ) ), game::get_instance(), SLOT( command_received( player*, std::string ) ) );
	map_array = new array2d< map_square* >( mapa::get_instance()->get_size_x(), mapa::get_instance()->get_size_y() );
	parameters* param = parameters::get_instance();

	path = 0;
	fire_line = 0;
	//remote = from ai of network
	remote_path = 0;
	remote_fire_line = 0;
	selected_unit = 0;



	QVBoxLayout* layout = new QVBoxLayout( this );



	QWidget* title_w = new QWidget( this );
	QHBoxLayout* title_w_l = new QHBoxLayout( title_w );


	current_player_l = new QLabel( title_w );
	current_player_l->setTextFormat( Qt::RichText );
	update_curr_player_label();
	title_w_l->addWidget( current_player_l );

	current_time_l = new QLabel( title_w );
	current_time_l->setTextFormat( Qt::RichText );
	update_curr_time_label();
	title_w_l->addWidget( current_time_l );

	QPushButton* save_b = new QPushButton( "Save game", title_w );
	save_b->setFixedWidth( 150 );
	title_w_l->addWidget( save_b );
	connect( save_b, SIGNAL( clicked() ), this, SLOT( save_game() ) );

	pause_b = new QPushButton( "AI Continue", title_w );
	pause_b->setFixedWidth( 150 );
	title_w_l->addWidget( pause_b );
	connect( pause_b, SIGNAL( clicked() ), this, SLOT( ai_pause() ) );

	next_ai_command_b = new QPushButton( "Next command", title_w );
	next_ai_command_b->setFixedWidth( 150 );
	next_ai_command_b->setEnabled( game::get_instance()->get_current_player()->get_type() == ai );
	title_w_l->addWidget( next_ai_command_b );
	connect( next_ai_command_b, SIGNAL( clicked() ), this, SLOT( next_ai_command() ) );

	next_b = new QPushButton( "NEXT", title_w );
	next_b->setFixedWidth( 150 );
	title_w_l->addWidget( next_b );
	connect( next_b, SIGNAL( clicked() ), this, SLOT( next() ) );


	title_w->setLayout( title_w_l );
	layout->addWidget( title_w );




	QWidget* map_play_w = new QWidget( this );
	QHBoxLayout* map_play_w_l = new QHBoxLayout( map_play_w );


	QGraphicsView *gv = new QGraphicsView( map_play_w );
	gv->setFixedSize( mapa::get_instance()->get_size_x() * param->gui_parameters[ "ICON_W" ] + 4, mapa::get_instance()->get_size_y() * param->gui_parameters[ "ICON_H" ] + 4 );

	gs=new QGraphicsScene( 0, 0, mapa::get_instance()->get_size_x() * param->gui_parameters[ "ICON_W" ], mapa::get_instance()->get_size_y() * param->gui_parameters[ "ICON_H" ], gv );
	gs->setBackgroundBrush( *( game_gui::get_instance()->graphics->map_background ) );
	
	gv->setScene( gs );
	map_play_w_l->addWidget( gv );


	create_map();
	add_bonuses();
	add_players();


	QWidget* units_survey_w = new QWidget( map_play_w );
	QHBoxLayout* units_survey_w_l = new QHBoxLayout( units_survey_w );

	QWidget** players_units_w = new QWidget*[ parameters::get_instance()->game_parameters[ "MAX_PL" ] ];
	QVBoxLayout* players_units_w_l;
	QLabel* player_name_l;
	std::ostringstream sout;
	
	for ( unsigned int i = 0; i < players::get_instance()->get_players_count(); ++i )
	{
		//player's units column
		players_units_w[ i ] = new QWidget( units_survey_w );
		players_units_w_l = new QVBoxLayout( players_units_w[ i ] );

		player_name_l = new QLabel( players_units_w[ i ] );
		player_name_l->setTextFormat( Qt::RichText );
		sout << "<font size=4><b>" << players::get_instance()->get_player( i )->get_name() << "</b></font>";
		player_name_l->setMinimumWidth( 70 );
		player_name_l->setText( sout.str().c_str() );
		sout.str( "" );
		players_units_w_l->addWidget( player_name_l );

		for ( unsigned int j = 0; j < players::get_instance()->get_player(i)->units.size(); ++j )
		{
			//player's units row
			QLabel* unlab = new QLabel( players_units_w[ i ] );
			unlab->setTextFormat( Qt::RichText );
			update_unit_status( players::get_instance()->get_player( i )->units[ j ], unlab );
			map_unit* mu = dynamic_cast< map_unit* >( gs->itemAt( players::get_instance()->get_player( i )->units[ j ]->get_x() * parameters::get_instance()->gui_parameters[ "ICON_W" ] + 1, players::get_instance()->get_player( i )->units[ j ]->get_y() * parameters::get_instance()->gui_parameters[ "ICON_H" ] + 1 ) );
			if ( mu != 0 )
			{		
				mu->setUnitInfo( unlab );
			}
			players_units_w_l->addWidget( unlab );
		}

		players_units_w[ i ]->setLayout( players_units_w_l );
		units_survey_w_l->addWidget( players_units_w[ i ] );
	};
	
	units_survey_w->setLayout( units_survey_w_l );
	map_play_w_l->addWidget( units_survey_w );


	map_play_w->setLayout( map_play_w_l );
	layout->addWidget( map_play_w );

	

	selected_unit_status_l = new QLabel( this );
	selected_unit_status_l->setTextFormat( Qt::RichText );
	update_selected_unit_status( 0 );
	layout->addWidget( selected_unit_status_l );
	


	QWidget* control_buttons_w = new QWidget( this );
	QHBoxLayout* control_buttons_w_l = new QHBoxLayout( control_buttons_w );


	QPushButton* show_estimated_places_b = new QPushButton( "Show estimated map places", control_buttons_w );
	control_buttons_w_l->addWidget( show_estimated_places_b );
	connect( show_estimated_places_b, SIGNAL( clicked() ), this, SLOT( show_estimated_places() ) );

	QPushButton* show_dangerous_places_b = new QPushButton( "Show dangerous map places", control_buttons_w );
	control_buttons_w_l->addWidget( show_dangerous_places_b );
	connect( show_dangerous_places_b, SIGNAL( clicked() ), this, SLOT( show_dangerous_places() ) );

	QPushButton* show_dangerous_places_nobody_b = new QPushButton( "Show possible dangerous map places", control_buttons_w );
	control_buttons_w_l->addWidget( show_dangerous_places_nobody_b );
	connect( show_dangerous_places_nobody_b, SIGNAL( clicked() ), this, SLOT( show_dangerous_places_nobody() ) );

	QPushButton* show_attackable_places_b = new QPushButton( "Show attackable map places", control_buttons_w );
	control_buttons_w_l->addWidget( show_attackable_places_b );
	connect( show_attackable_places_b, SIGNAL( clicked() ), this, SLOT( show_attackable_places() ) );

	QPushButton* show_game_state_score_b = new QPushButton( "Show game state score", control_buttons_w );
	control_buttons_w_l->addWidget( show_game_state_score_b );
	connect( show_game_state_score_b, SIGNAL( clicked() ), this, SLOT( show_game_state_score() ) );


	control_buttons_w->setLayout( control_buttons_w_l );
	layout->addWidget( control_buttons_w );



	this->setLayout( layout );
};



void play_widget::create_map()
{
	parameters* param = parameters::get_instance();
	mapa* map = mapa::get_instance();
	game_gui* ggui = game_gui::get_instance();
	for ( unsigned int i = 0; i < mapa::get_instance()->get_size_x(); ++i )
		for ( unsigned int j = 0; j < mapa::get_instance()->get_size_y(); ++j )
		{
			map_array->set( new map_square(), i, j );
			map_array->get( i, j )->setPixmap( *ggui->graphics->terrain_pixmaps[ ( ( map->terrain->get( i, j ) / 10 )* 10 ) ] );
			gs->addItem( map_array->get( i, j ) );
			map_array->get( i, j )->setPos( i * param->gui_parameters[ "ICON_W" ], j * param->gui_parameters[ "ICON_H" ] );
		}
};



void play_widget::add_bonuses()
{
	parameters* param = parameters::get_instance();
	for ( unsigned int i = 0; i < mapa::get_instance()->get_size_x(); ++i )
		for ( unsigned int j = 0; j < mapa::get_instance()->get_size_y(); ++j )
		{
			bonus* b = dynamic_cast< bonus* >( mapa::get_instance()->get_object( i, j ) );
			if ( b != 0 )
			{
				map_bonus* mb = new map_bonus( b );
				gs->addItem( mb );
				mb->setPos( b->get_x() * param->gui_parameters[ "ICON_W" ], b->get_y() * param->gui_parameters[ "ICON_H" ] );
			}
		}
};



void play_widget::add_players()
{
	player* pl;
	map_unit* mu;
	for ( unsigned int i = 0; i < players::get_instance()->get_players_count(); ++i )
	{
		pl = players::get_instance()->get_player( i );
		for ( unsigned int j = 0; j < pl->units.size(); ++j )
		{
			mu = new map_unit( pl->units[j] );
			gs->addItem( mu );
			mu->setPos( mu->u->get_x() * parameters::get_instance()->gui_parameters[ "ICON_W" ], mu->u->get_y() * parameters::get_instance()->gui_parameters[ "ICON_H" ] );
		}
	}
};



void play_widget::update_curr_player_label()
{
	std::ostringstream sout;
	sout << "<font size=6>" << game::get_instance()->get_current_player()->get_name() << " :</font>";
	current_player_l->setText( sout.str().c_str() );
};



void play_widget::update_curr_time_label()
{
	std::ostringstream sout;
	sout << "<font size=5>Time left: " << game::get_instance()->get_current_player()->get_time() << "</font>";
	current_time_l->setText( sout.str().c_str() );
};



void play_widget::update_unit_status( unit* u, QLabel* ul )
{
	std::ostringstream sout;
	sout << "<font size=4>"  << u->get_name() << " " << u->get_health() << " [" << u->get_x() << "," << u->get_y() << "]</font>";
	ul->setText( sout.str().c_str() );
};



void play_widget::update_selected_unit_status( unit* u )
{
	std::ostringstream sout;
	if ( u != 0 )
		sout << "<font size=5><b>SELECTED UNIT:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b></font><font size=4><b>" << u->get_player()->get_name() << "'s " << u->get_name() << "&nbsp;&nbsp;&nbsp;Range: </b>" << u->get_range() << "<b>&nbsp;&nbsp;&nbsp;Health: </b>" << u->get_health() << "<b>&nbsp;&nbsp;&nbsp;Shield: </b>" << u->get_shield() << "<b>&nbsp;&nbsp;&nbsp;Weapon: </b>" << u->get_weapon() << "<b>&nbsp;&nbsp;&nbsp;Cost move: </b>" << u->get_cost_move() << "<b>&nbsp;&nbsp;&nbsp;Cost fire: </b> " << u->get_cost_fire() << "<b>&nbsp;&nbsp;&nbsp;Position: </b>[" << u->get_x() << "," << u->get_y() << "]</font>";
	else
		sout << "<font size=5><b>NO UNIT SELECTED</b></font>";
	selected_unit_status_l->setText( sout.str().c_str() );
};



void play_widget::show_move( unit* u, unsigned int x, unsigned int y, pre_move_info* pmi )
{	
	if ( remote_path != 0 )
	{
		delete remote_path;
		remote_path = 0;
	}
	parameters* param = parameters::get_instance();
	if ( path != 0 )
	{
		delete path;
		path = 0;
	}
	//was there a bonus?
	map_bonus* mb = ( dynamic_cast< map_bonus* >( gs->itemAt( x * param->gui_parameters[ "ICON_W" ] + 1 , y * param->gui_parameters[ "ICON_H" ] + 1 ) ) );
	if ( mb != 0 )
		delete mb;
	std::queue< std::pair< unsigned int, unsigned int > >* pth = pmi->path;
	// u moved from [ox,oy]
	unsigned int ox = pmi->ox;
	unsigned int oy = pmi->oy;
	//u is selected
	if ( ( selected_unit != 0 ) && ( selected_unit->u == u ) )
		update_selected_unit_status( selected_unit->u );
	update_curr_time_label();
	//moves units icon to actual place
	map_unit* mu = ( dynamic_cast< map_unit* >( gs->itemAt( ox * param->gui_parameters[ "ICON_W" ] + 1 , oy * param->gui_parameters[ "ICON_H" ] + 1 ) ) );
	if ( mu != 0 )
	{
		mu->setPos( x * param->gui_parameters[ "ICON_W" ], y * param->gui_parameters[ "ICON_H" ] );
		update_unit_status( u, mu->uinfo );
	}
	//u is network or ai player's -> show path forced
	if( u->get_player()->get_type() != local )
	{
		remote_path = new p( pth );
		QTimer::singleShot( ( parameters::get_instance()->ai_parameters[ "GENERAL" ][ "ai_delay" ] * 0.9 ), this, SLOT( delete_remote_path() ) );
	}
};



void play_widget::delete_remote_path()
{
	if ( remote_path != 0 )
	{
		delete remote_path;
		remote_path = 0;
	}
};



void play_widget::show_fire( unit* u, unsigned int x, unsigned int y, pre_fire_info* pfi )
{
	if ( remote_fire_line != 0 )
	{
		delete remote_fire_line;
		remote_fire_line = 0;
	}
	//targed destroyed of unattackable
	if ( ( mapa::get_instance()->get_object( x, y ) == 0 ) || ( ( mapa::get_instance()->get_object( x, y ) != 0 ) && !( test_fire( u, x, y ) ) ) )
	{
		if ( fire_line != 0 )
		{
			delete fire_line;
			fire_line = 0;
		}
	}
	//targed selected
	bool s = pfi->s;
	unsigned int ICON_W = parameters::get_instance()->gui_parameters[ "ICON_W" ];
	unsigned int ICON_H = parameters::get_instance()->gui_parameters[ "ICON_H" ];
	mapa* map = mapa::get_instance();
	update_curr_time_label();
	//mu - attacking unit
	map_unit* mu = ( dynamic_cast< map_unit* >( gs->itemAt( u->get_x() * ICON_W + 1, u->get_y() * ICON_H + 1 ) ) );
	//because of fire line
	if ( mu != 0 )
		mu->playFireSound();
	else
	{
		mu = ( dynamic_cast< map_unit* >( gs->itemAt( u->get_x() * ICON_W + ICON_W - 1, u->get_y() * ICON_H + ICON_H - 1 ) ) );
		if ( mu != 0 )
			mu->playFireSound();
	}
	//target destroyed
	if ( map->get_object( x, y ) == 0 )
	{
		map_unit* mu = ( dynamic_cast< map_unit* >( gs->itemAt( x * ICON_W + ICON_W - 1, y * ICON_H + ICON_H - 1 ) ) );
		//because of fire line
		if ( mu != 0 )
		{
			delete mu->uinfo;
			delete mu;
		}
		else
		{
			mu = ( dynamic_cast< map_unit* >( gs->itemAt( x * ICON_W + 1, y * ICON_H + 1 ) ) );
			if ( mu != 0 )
			{
				delete mu->uinfo;
				delete mu;
			}
		}
		//targed selected
		if ( s )
		{
			update_selected_unit_status( 0 );
			selected_unit = 0;
		}
	}
	//targed alived
	else
	{
		unit* uu = dynamic_cast< unit* >( map->get_object( x, y ) );
		map_unit* mu = ( dynamic_cast< map_unit* >( gs->itemAt( x * ICON_W + 1, y * ICON_H + 1 ) ) );
		//because of fire line
		if ( mu != 0 )		
		{
			update_unit_status( uu, mu->uinfo );
		}
		else
		{
			mu = ( dynamic_cast< map_unit* >( gs->itemAt( x * ICON_W + ICON_W - 1, y * ICON_H + ICON_H - 1 ) ) );
			if ( mu != 0 )
				update_unit_status( uu, mu->uinfo );
		}
		if ( ( selected_unit != 0 ) && ( selected_unit->u == ( dynamic_cast< unit* >( map->get_object( x, y ) ) ) ) )
			update_selected_unit_status( selected_unit->u );

	}
	if ( ( selected_unit != 0 ) && ( selected_unit->u == u ) )
		update_selected_unit_status( selected_unit->u );
	//u is network or ai player's -> show fire line forced
	if ( u->get_player()->get_type() != local )
	{
		remote_fire_line = new QGraphicsLineItem( u->get_x() * ICON_W + ICON_W / 2, u->get_y() * ICON_H + ICON_H / 2, x * ICON_W + ICON_W / 2, y * ICON_H + ICON_H / 2 );
		remote_fire_line->setZValue( 100 );
		remote_fire_line->setPen( *( game_gui::get_instance()->graphics->fire ) );
		gs->addItem( remote_fire_line );
		QTimer::singleShot( parameters::get_instance()->ai_parameters[ "GENERAL" ][ "ai_delay" ] * 0.9, this, SLOT( delete_remote_fire_line() ) );
	}
};



void play_widget::delete_remote_fire_line()
{
	if ( remote_fire_line != 0 )
	{
		delete remote_fire_line;
		remote_fire_line = 0;
	}
};



void play_widget::show_next_turn()
{
	if ( fire_line != 0 )
	{
		delete fire_line;
		fire_line = 0;
	}
	if ( path != 0 )
	{
		delete path;
		path = 0;
	}

	next_ai_command_b->setEnabled( ( game::get_instance()->is_paused() ) && ( game::get_instance()->get_current_player()->get_type() == ai ) );

	update_curr_player_label();
	update_curr_time_label();
};



void play_widget::set_paused( bool paused )
{
	next_ai_command_b->setEnabled( paused && ( game::get_instance()->get_current_player()->get_type() == ai ) );
	if ( paused )
		pause_b->setText( "AI Continue" );
	else
		pause_b->setText( "AI Pause" );	
};



void play_widget::next()
{
	game::get_instance()->next_turn();
};



void play_widget::save_game()
{
	mapa::get_instance()->save_objects( "saved_game.txt" );
};


void play_widget::ai_pause()
{
	game::get_instance()->set_paused( !game::get_instance()->is_paused() );
};



void play_widget::next_ai_command()
{
	game::get_instance()->get_next_ai_command_now();
};



void play_widget::show_estimated_places()
{
	mapa::export_map( game::get_instance()->get_current_state()->estimated_places, "estimated_places.txt", mapa::get_instance()->get_size_x(), mapa::get_instance()->get_size_y() );
	QWidget* map = create_map_widget( game::get_instance()->get_current_state()->estimated_places );
	map->show();
};



void play_widget::show_dangerous_places()
{
	mapa::export_map( game::get_instance()->get_current_state()->dangerous_places, "dangerous_places.txt", mapa::get_instance()->get_size_x(), mapa::get_instance()->get_size_y() );
	QWidget* map = create_map_widget( game::get_instance()->get_current_state()->dangerous_places );
	map->show();
};



void play_widget::show_dangerous_places_nobody()
{
	mapa::export_map( game::get_instance()->get_current_state()->dangerous_places_nobody, "dangerous_places_nobody.txt", mapa::get_instance()->get_size_x(), mapa::get_instance()->get_size_y() );
	QWidget* map = create_map_widget( game::get_instance()->get_current_state()->dangerous_places_nobody );
	map->show();
};



void play_widget::show_attackable_places()
{
	mapa::export_map( game::get_instance()->get_current_state()->attackable_places, "attackable_places.txt", mapa::get_instance()->get_size_x(), mapa::get_instance()->get_size_y() );
	QWidget* map = create_map_widget( game::get_instance()->get_current_state()->attackable_places );
	map->show();
};



void play_widget::show_game_state_score()
{
	QLabel* score_l = new QLabel();
	score_l->setNum( game::get_instance()->get_current_state()->score ); 
	score_l->show();
};



QWidget* play_widget::create_map_widget( array2d< unsigned int >* map )
{
	QWidget* created_map_w = new QWidget();
	QLabel* num_l;
	QGridLayout* layout = new QGridLayout( created_map_w );
	unsigned int max = 0;
	for ( unsigned int i = 0; i < mapa::get_instance()->get_size_x(); ++i )
		for ( unsigned int j = 0; j < mapa::get_instance()->get_size_y(); ++j )
			if ( map->get( i, j ) > max )
				max = map->get( i, j );
	if ( max == 0 )
		max = 1;
	for ( unsigned int i = 0; i <mapa::get_instance()->get_size_x(); ++i )
		for ( unsigned int j = 0; j < mapa::get_instance()->get_size_y(); ++j )
		{
			num_l = new QLabel( created_map_w );
			num_l->setAutoFillBackground( true );
			//computing rgb parameters
			unsigned int r = floor( ( sqrt( ( double )map->get( i, j ) / ( double )max ) * 255 ) + 0.5 );
			unsigned int g = floor( ( pow( ( ( double )map->get( i, j ) / ( double )max ), 3 ) * 255 ) + 0.5 );
			int b = floor( ( sin( ( ( double )map->get( i, j ) / ( double )max ) * 2 * 3.14159265 ) * 255 ) + 0.5 );
			if ( b < 0 )
				b = 0;
			num_l->setTextFormat( Qt::RichText );
			std::ostringstream sout;
			sout << "<font color=#000000><b>" << map->get( i, j ) << "</b></font>";
			num_l->setText( sout.str().c_str() );
			num_l->setPalette( QPalette( QColor( r, g, b ) ) );
			num_l->setAlignment( Qt::AlignCenter );
			num_l->setMargin( 2 );
			layout->addWidget( num_l, j, i );
		}
	created_map_w->setLayout( layout );
	return created_map_w;
};